package com.example.LibrarySystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.LibrarySystem.mapper.ClassesMapper;
import com.example.LibrarySystem.pojo.Classes;
import com.example.LibrarySystem.pojo.User;
import com.example.LibrarySystem.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ClassesServiceImpl implements ClassesService {

    @Autowired
    private ClassesMapper classesMapper;

    /**
     * 添加一个班级
     * @param classes
     * @return
     */
    @Override
    public int insert(Classes classes) {
        return classesMapper.insert(classes);
    }

    @Override
    public IPage<Classes> getClassesByAll(Map<String, String> data) {
        int start = data.containsKey("start") ? Integer.parseInt(data.get("start")) : 1;
        int pages = data.containsKey("pages") ? Integer.parseInt(data.get("pages")) : 10;

        IPage<Classes> ClassesIPage = new Page<>(start, pages);

        QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();

        // 判断传入的参数有没有name这个字段 有就进行模糊查询
        if (data.containsKey("name") && !"".equals(data.get("name"))) {
            queryWrapper.like("name", data.get("name"));
        }
        IPage<Classes> res = classesMapper.selectPage(ClassesIPage,queryWrapper);
        return res;
    }
}
